package com.game77.guessTheWords2.sprite;

import com.game77.guessTheWords2.core.GameSystem;
import com.game77.guessTheWords2.core.StageManager;
import com.game77.guessTheWords2.entity.MatrixCharInfo;
import com.game77.guessTheWords2.entity.StageData;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYRect;
import com.wiyun.engine.utils.ResolutionIndependent;

/**
 * 字符块
 * 
 * @author davelin
 * 
 */
public class CharacterBlock extends Node {

	Sprite background;
	Label showChar;

	boolean isCompleted = false;

	// 当前此block的状态
	int currentType = 0;

	int row;
	int column;
	int horizonWordIndex;
	int verticalWordIndex;

	String horizontalTip = "";
	String verticalTip = "";

	/**
	 * 横向目标拼音
	 */
	String aimPy;

	/**
	 * 竖向目标拼音
	 */
	String aimPy2;
	String aimChinese;

	String currentPy = "";

	public int getHorizonWordIndex() {
		return horizonWordIndex;
	}

	public void setHorizonWordIndex(int horizonWordIndex) {
		this.horizonWordIndex = horizonWordIndex;
	}

	public int getVerticalWordIndex() {
		return verticalWordIndex;
	}

	public void setVerticalWordIndex(int verticalWordIndex) {
		this.verticalWordIndex = verticalWordIndex;
	}

	public String getVerticalTip() {
		return verticalTip;
	}

	public void setVerticalTip(String verticalTip) {
		this.verticalTip = verticalTip;
	}

	public String getHorizontalTip() {
		return horizontalTip;
	}

	public void setHorizontalTip(String horizontalTip) {
		this.horizontalTip = horizontalTip;
	}

	public int getRow() {
		return row;
	}

	public void setRow(int row) {
		this.row = row;
	}

	public int getColumn() {
		return column;
	}

	public void setColumn(int column) {
		this.column = column;
	}

	public String getAimPy() {
		return aimPy;
	}

	public String getAimChinese() {
		return aimChinese;
	}

	/**
	 * 是否是完成状态
	 * 
	 * @return
	 */
	public boolean isCompleted() {
		return isCompleted;
	}

	/**
	 * 设置成完成状态
	 */
	public void setComplete() {
		isCompleted = true;
		showChar.setText(aimChinese);

		// 设置为完成状态
		StageData sd = StageManager.instance.getCurrentStageData();
		MatrixCharInfo mci = new MatrixCharInfo();
		mci.setRow(row);
		mci.setColumn(column);
		mci.setComplete(true);
		sd.getCharInfoList().put(row * GameSystem.GameColumns + column, mci);
	}

	/**
	 * 设置目标横向
	 */
	public void setAimHorizon(String py, String chinese) {
		aimChinese = chinese;
		aimPy = py;
	}

	/**
	 * 设置目标竖向
	 */
	public void setAimVertical(String py, String chinese) {
		aimChinese = chinese;
		aimPy2 = py;
	}

	public int getCurrentType() {
		return currentType;
	}

	public CharacterBlock() {
		background = Sprite.make(GameSystem.tex_char_background,
				getCharBackgroundRect(0));
		background.setPosition(0, 0);
		showChar = Label.make("", GameSystem.CharBlockFontSize);
		showChar.setColor(WYColor3B.make(0, 0, 0));
		showChar.setPosition(0, 0);
		addChild(background);
		addChild(showChar);
	}

	private WYRect getCharBackgroundRect(int x) {
		return ResolutionIndependent.resolve(WYRect.make(x
				* GameSystem.CharSize, 0, GameSystem.CharSize,
				GameSystem.CharSize));
	}

	/**
	 * 切换背景类型
	 * 
	 * @param type
	 *            0-normal, 1-nochar, 2-selected, 3-senseHorizon,
	 *            4-senseVertical
	 */
	public void changeBackgroundType(int type) {
		background.setTextureRect(getCharBackgroundRect(type));
		showChar.setVisible(type != BlockType.NoChar);
		currentType = type;
	}

	public void setShowChar(String str) {
		// 如果已经完成了该词条，无需更新
		if (!isCompleted) {
			showChar.setText(str);
			currentPy = str;
			// 设置为完成状态
			StageData sd = StageManager.instance.getCurrentStageData();
			MatrixCharInfo mci = new MatrixCharInfo();
			mci.setRow(row);
			mci.setColumn(column);
			mci.setComplete(false);
			mci.setCharStr(str);
			sd.getCharInfoList()
					.put(row * GameSystem.GameColumns + column, mci);
		}
	}

	public void clearBlock() {
		changeBackgroundType(BlockType.NoChar);

		isCompleted = false;
		horizonWordIndex = 0;
		verticalWordIndex = 0;

		horizontalTip = "";
		verticalTip = "";

		aimPy = null;
		aimPy2 = null;
		aimChinese = null;
		currentPy = "";
		showChar.setText("");
	}

	/**
	 * 是否填写正确
	 * 
	 * @return
	 */
	public boolean isCorrectPy() {
		if (isCompleted) {
			return true;
		} else {
			return currentPy.equals(aimPy) || currentPy.equals(aimPy2);
		}
	}

	public class BlockType {
		public static final int Normal = 0;
		public static final int NoChar = 1;
		public static final int Selected = 2;
		public static final int SenseHorizon = 3;
		public static final int SenseVertical = 4;
	}
}
